//
// Created by lenovo on 2024/1/11.
//

#include "Tokenizer.h"
#include "DoubleConverter.h"
std::vector<token *> Tokenizer::result() {
    if (not _done)
        this->convert();
    this->_done = true;

    return this->tokens;
}

#include <iostream>
//using namespace std ;

void Tokenizer::convert() {
    for (
            auto str : this->material
            )
    {
        if ( str == "(" ){
            this->tokens.push_back(new token(token::Type::LBRACKET));
        }
        else if (str == ")"){
            this->tokens.push_back(new token(token::Type::RBRACKET));
        }
        else if (
                Character::Nums.find(str.front()) != Character::Nums.end()
                )
        {
            //暂时不能排错
            DoubleConverter dcv (str);
//            cout << "Number : " << dcv.result() << endl;
            this->tokens.push_back(
                    new token(
                            token::Type::NUMBER,
                            new Num(dcv.result())
                    )
            );
        }
        else if (
                Character::Operators.find(str) != Character::Operators.end()
                )
        {
//            cout << "An Operator is now Tokenizing" << endl;
            this->tokens.push_back(
                    new token(
                            token::Type::OPERATOR,
                            new Operator(str)
                            )
                    );
        }
//        else if (
//                Character::Funcs.find(str) != Character::Funcs.end()
//                )
//        {
////            cout << "Funcs" << endl;
//            this->tokens.push_back(
//                    new token(
//
//                            token::Type::OPERATOR,
//                            new Funcs(str)
//                    )
//            );
//        }
        else if (
                str == ","
                )
        {
            this->tokens.push_back(
                    new token(
                            token::Type::DOT
                            )
                    );
        }
        else{
            //其余情况均识别为Function
            //至于能否运算，并非本阶段的职责
            this->tokens.push_back(
                    new token(
                            token::Type::FUNCTION,
                            new Funcs(str , nullptr)
                            )
                    );

        }
//            throw std::invalid_argument("Invalid Token");
    }
}
